﻿using Newtonsoft.Json;
using RTW.OA.Areas.Common.Servers;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Common.Controllers
{
    public class VehicleController : Controller
    {
        private CarServers carServers;
        private CarUseServers caruseServers;
        private CarMaintenanceServers carmaintenanceServers;
        private CarOperatorServers caroperatorServers;

        public VehicleController()
        {
            carServers = new CarServers();
            caruseServers = new CarUseServers();
            carmaintenanceServers = new CarMaintenanceServers();
            caroperatorServers = new CarOperatorServers();
        }

        // GET: Common/Vehicle
        public ActionResult Car()
        {
            ViewBag.V_TYPEData = JsonConvert.SerializeObject(carServers.GetV_TYPE()); 
            return View();
        }

        public ActionResult CarUse()
        {
            ViewBag.V_IDData = JsonConvert.SerializeObject(caruseServers.GetV_ID()); 
            ViewBag.VU_USERData = JsonConvert.SerializeObject(caruseServers.GetVU_USER()); 
            ViewBag.VU_SUITEData = JsonConvert.SerializeObject(caruseServers.GetVU_SUITE()); 
            return View();
        }

        public ActionResult CarMaintenance()
        {
            ViewBag.V_IDDataList = carmaintenanceServers.GetV_ID();
            ViewBag.VM_TYPEDataList = carmaintenanceServers.GetVM_TYPE();
            ViewBag.V_IDData = JsonConvert.SerializeObject(carmaintenanceServers.GetV_ID()); 
            ViewBag.VM_TYPEData = JsonConvert.SerializeObject(carmaintenanceServers.GetVM_TYPE()); 
            ViewBag.VM_PERSONData = JsonConvert.SerializeObject(carmaintenanceServers.GetVM_PERSON()); 
            return View();
        }

        // GET: Common/CarOperator/Edit/5
        public ActionResult CarOperatorEdit(int? id)
        {
            var model = new vehicle_operator();
            if (id != null)
            {
                ViewBag.Title = "编辑车辆调度信息";
                model = GetCarOperatorModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建车辆调度信息";
            }
            return View(model);
        }

        // POST: Common/CarOperator/Edit/5
        [HttpPost]
        public ActionResult CarOperatorEdit(int? id, vehicle_operator model)
        {
            try
            {
                if (id == null || id == 0)
                {
                    caroperatorServers.Add(model);
                    ViewBag.msg = "新增车辆调度信息成功！";
                    model = new vehicle_operator();
                }
                else
                {
                    caroperatorServers.Update(model);
                    ViewBag.msg = "修改车辆调度信息成功！";
                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误(" + ex.ToString() + ")！";
            }

            return View(model);
        }
        
        // GET: Common/CarMaintenance/Edit/5
        public ActionResult CarMaintenanceEdit(int? id)
        {
            ViewBag.V_IDData = carmaintenanceServers.GetV_ID();
            ViewBag.VM_TYPEData = carmaintenanceServers.GetVM_TYPE();
            ViewBag.VM_PERSONData = carmaintenanceServers.GetVM_PERSON();
            var model = new vehicle_maintenance();
            if (id != null)
            {
                ViewBag.Title = "编辑车辆维护信息";
                model = GetCarMaintenanceModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建车辆维护信息";
            }
            return View(model);
        }

        // POST: Common/CarMaintenance/Edit/5
        [HttpPost]
        public ActionResult CarMaintenanceEdit(int? id, vehicle_maintenance model)
        {
            ViewBag.V_IDData = carmaintenanceServers.GetV_ID();
            ViewBag.VM_TYPEData = carmaintenanceServers.GetVM_TYPE();
            ViewBag.VM_PERSONData = carmaintenanceServers.GetVM_PERSON();
            try
            {
                if (id == null || id == 0)
                {
                    carmaintenanceServers.Add(model);
                    ViewBag.msg = "新增车辆维护信息成功！";
                    model = new vehicle_maintenance();
                }
                else
                {
                    carmaintenanceServers.Update(model);
                    ViewBag.msg = "修改车辆维护信息成功！";

                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误(" + ex.ToString() + ")！";
            }

            return View(model);
        }
        
        // GET: Common/CarUse/Edit/5
        public ActionResult CarUseEdit(int? id)
        {
            ViewBag.V_IDData = caruseServers.GetV_ID();
            ViewBag.VU_USERData = caruseServers.GetVU_USER();
            ViewBag.VU_DEPTData = caruseServers.GetVU_DEPT();
            ViewBag.VU_OPERATORData = caruseServers.GetVU_OPERATOR();
            ViewBag.SMS2_REMINDData = caruseServers.GetSMS2_REMIND();
            ViewBag.DEPT_MANAGERData = caruseServers.GetDEPT_MANAGER();
            ViewBag.VU_SUITEData = caruseServers.GetVU_SUITE();
            ViewBag.VU_OPERATOR1Data = caruseServers.GetVU_OPERATOR1();
            ViewBag.VU_OPERATOR1_SMS_TYPEData = caruseServers.GetVU_OPERATOR1_SMS_TYPE();
            var model = new vehicle_usage();
            if (id != null)
            {
                ViewBag.Title = "编辑车辆使用信息";
                model = GetCarUseModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建车辆使用信息";
            }
            return View(model);
        }

        // POST: Common/CarUse/Edit/5
        [HttpPost]
        public ActionResult CarUseEdit(int? id, vehicle_usage model)
        {
            ViewBag.V_IDData = caruseServers.GetV_ID();
            ViewBag.VU_USERData = caruseServers.GetVU_USER();
            ViewBag.VU_DEPTData = caruseServers.GetVU_DEPT();
            ViewBag.VU_OPERATORData = caruseServers.GetVU_OPERATOR();
            ViewBag.SMS2_REMINDData = caruseServers.GetSMS2_REMIND();
            ViewBag.DEPT_MANAGERData = caruseServers.GetDEPT_MANAGER();
            ViewBag.VU_SUITEData = caruseServers.GetVU_SUITE();
            ViewBag.VU_OPERATOR1Data = caruseServers.GetVU_OPERATOR1();
            ViewBag.VU_OPERATOR1_SMS_TYPEData = caruseServers.GetVU_OPERATOR1_SMS_TYPE();
            try
            {
                if (id == null || id == 0)
                {
                    caruseServers.Add(model);
                    ViewBag.msg = "新增车辆使用信息成功！";
                    model = new vehicle_usage();

                }
                else
                {
                    caruseServers.Update(model);
                    ViewBag.msg = "修改车辆使用信息成功！";

                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误(" + ex.ToString() + ")！";
            }

            return View(model);
        }
        // GET: Common/Vehicle/Edit/5
        public ActionResult CarEdit(int? id)
        {
            ViewBag.V_TYPEData = carServers.GetV_TYPE();
            ViewBag.CAR_USERData = carServers.GetCAR_USER();
            ViewBag.V_MOT_SMS_TYPEData = carServers.GetV_MOT_SMS_TYPE();
            ViewBag.V_INSURE_SMS_TYPEData = carServers.GetV_INSURE_SMS_TYPE();
            ViewBag.V_DEPARTData = carServers.GetV_DEPART();
            ViewBag.V_ONWERData = carServers.GetV_ONWER();
            ViewBag.V_CARUSERData = carServers.GetV_CARUSER();
            ViewBag.V_NATUREData = carServers.GetV_NATURE();
            ViewBag.V_BINSURE_SMS_TYPEData = carServers.GetV_BINSURE_SMS_TYPE();
            var model = new vehicle_car();
            if (id != null)
            {
                ViewBag.Title = "编辑车辆信息";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建车辆信息";
            }
            return View(model);
        }

        // POST: Common/Vehicle/Edit/5
        [HttpPost]
        public ActionResult CarEdit(int? id, vehicle_car model)
        {
            ViewBag.V_TYPEData = carServers.GetV_TYPE();
            ViewBag.CAR_USERData = carServers.GetCAR_USER();
            ViewBag.V_MOT_SMS_TYPEData = carServers.GetV_MOT_SMS_TYPE();
            ViewBag.V_INSURE_SMS_TYPEData = carServers.GetV_INSURE_SMS_TYPE();
            ViewBag.V_DEPARTData = carServers.GetV_DEPART();
            ViewBag.V_ONWERData = carServers.GetV_ONWER();
            ViewBag.V_CARUSERData = carServers.GetV_CARUSER();
            ViewBag.V_NATUREData = carServers.GetV_NATURE();
            ViewBag.V_BINSURE_SMS_TYPEData = carServers.GetV_BINSURE_SMS_TYPE();
            try
            {
                if (id == null || id == 0)
                {
                    carServers.Add(model);
                    ViewBag.msg = "新增车辆信息成功！";
                    model = new vehicle_car();

                }
                else
                {
                    carServers.Update(model);
                    ViewBag.msg = "修改车辆信息成功！";

                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误(" + ex.ToString() + ")！";
            }

            return View(model);
        }

        // POST: Common/Vehicle/Delete/5
        public ActionResult CarDelete(int id)
        {
            try
            {
                if (id != 0)
                {
                    carServers.Del(id.ToString());
                    ViewBag.msg = "删除车辆信息成功！";
                }
                return RedirectToAction("Car");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult CarMaintenanceDelete(int id)
        {
            try
            {
                if(id != 0)
                {
                    carmaintenanceServers.Del(id.ToString());
                    ViewBag.msg = "删除车辆维护信息成功！";
                }
                else
                {
                    ViewBag.msg = "删除车辆维护信息失败！";
                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误(" + ex.ToString() + ")！";
            }
            return RedirectToAction("CarMaintenance");
        }

        public ActionResult CarUseDelete(int id)
        {
            try
            {
                if (id != 0)
                {
                    caruseServers.Del(id.ToString());
                    ViewBag.msg = "删除车辆使用信息成功！";
                }
                else
                {
                    ViewBag.msg = "删除车辆使用信息失败！";
                }
            }
            catch (Exception ex)
            {
                ViewBag.msg = "提交出现错误(" + ex.ToString() + ")！";
            }
            return RedirectToAction("CarUse");
        }

        #region //GetData
        public string GetTableJsonResult()
        {
            var list = carServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public vehicle_car GetModel(string id)
        {
            var model = carServers.GetModelById(id);
            return model;
        }

        public string GetCarUseTableJsonResult()
        {
            var list = caruseServers.GetData();
            return JsonConvert.SerializeObject(list);
        }

        public vehicle_usage GetCarUseModel(string id)
        {
            var model = caruseServers.GetModelById(id);
            return model;
        }

        public string GetCarMaintenanceTableJsonResult(string V_ID,string VM_TYPE)
        {
            var list = carmaintenanceServers.GetData( V_ID, VM_TYPE);
            return JsonConvert.SerializeObject(list);
        }

        public vehicle_maintenance GetCarMaintenanceModel(string id)
        {
            var model = carmaintenanceServers.GetModelById(id);
            return model;
        }
        public vehicle_operator GetCarOperatorModel(string id)
        {
            var model = caroperatorServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}
